<?php
/**
 * User: lsx
 * Date: 2017/4/22
 * Time: 10:11
 * Progress:0%
 */

namespace logic\admin\wechat;

use model\WechatTemplateMessage;
use model\WechatTemplateVariable;
use think\Db;
use validate\WechatVariableCategory;

class LWechatTemplateMessage
{
    private $model;
    function __construct()
    {
        $this->model=new WechatTemplateMessage();
    }
    public function index_data($param){
        $where=array();
        if (!empty($keywords)) {
            $where['name'] = array('like', "%{$keywords}%"); //查询条件
        }
        $total = db('wechat_template_message')->where($where)->count(); //记录总数
        $page_size =$param['limit']; //获取分页数
        $start = $param['offset']; //获取开始的记录数
        //获取排序字段------------------------------------------------------------开始

        $sort_name = $param['ordinal'];
        $sort_desc = $param['order'];
        //$order[$sort_name] = $sort_desc;

        if (!empty($sort_name)) {
            $sort[$sort_name] = $sort_desc;
        } else {
            $sort['status'] = 'desc'; //默认排序
        }
        //获取排序字段------------------------------------------------------------结束
        $rows = db('wechat_template_message')
            ->limit($start, $page_size)
            ->order($sort)
            ->select();
        $array=array(
            'total'=>$total,
            'rows'=>$rows
        );

        return $array;
    }
    public function insert_data($param){
        $content=array();
        //构造消息内容
        $value_arr=array();
        $tem_short_id=$param['template_short_id'];
        $cate_id=Db::name('wechat_variable_category')->where(['tem_short_id'=>$tem_short_id])->find();

        $variables=Db::name('wechat_template_variable')->where(['category_id'=>$cate_id['id']])->select();
        $content['first']=array('value'=>input('first'),'color'=>'#000000');
        foreach ($variables as $variable) {
            $content[$variable['key']]=array('value'=>$variable['variable'],'color'=>$variable['color']);
        }

        $content['remark']=array('value'=>input('remark'),'color'=>'#000000');
        $param['content']=json_encode($content);
        $res=$this->model->save($param);
        if($res){
            return array(
                'success'=>true,
                'message'=>'保存成功'
            );
        }else{
            return array(
                'success'=>false,
                'message'=>'保存失败'
            );
        }

    }
//    public function insert_data($param,$key,$value,$color){
//        $content=array();
//        //构造消息内容
//        $value_arr=array();
//        for($j=0;$j<count($value);$j++){
//            $item=Db::name('wechat_template_variable')->where(['text'=>$value[$j]])->find();
//            $value_arr[]=$item['variable'];
//        }
//
//        $content['first']=array('value'=>input('first'),'color'=>'#000000');
//        for ($i=0;$i<count($key);$i++){
//            $content[$key[$i]]=array('value'=>str_replace('"', '', $value_arr[$i]),'color'=>$color[$i]);
//        }
//        $content['remark']=array('value'=>input('remark'),'color'=>'#000000');
//        $param['content']=json_encode($content);
//        $res=$this->model->save($param);
//        if($res){
//            return array(
//                'success'=>true,
//                'message'=>'保存成功'
//            );
//        }else{
//            return array(
//                'success'=>false,
//                'message'=>'保存失败'
//            );
//        }
//
//    }

    public function edit_index($id){
        $item=$this->model->where(['id'=>$id])->find();
        $item=$item->getData();
        $new_arr=array();
        $arr=array();
        $model=new WechatTemplateVariable();
        if($item['content']) {
            foreach (json_decode($item['content'],true) as $key=>$value){
                if($key!="first" && $key!="remark"){
                    $new_arr['key']=$key;
                    $valiable=$model->where(['variable'=>$value['value']])->find();
                    $new_arr['value']=$valiable['text'];
                    $new_arr['color']=$value['color'];
                    $item['data'][]=$new_arr;
                }else if($key=="first"){
                    $item['first']=$value['value'];
                }else if($key=="remark"){
                    $item['remark']=$value['value'];
                }
            }
        }
        return $item;
    }
    public function update_data($param,$id){
        $content=array();
        //构造消息内容
        $value_arr=array();
        $tem_short_id=$param['template_short_id'];
        $cate_id=Db::name('wechat_variable_category')->where(['tem_short_id'=>$tem_short_id])->find();

        $variables=Db::name('wechat_template_variable')->where(['category_id'=>$cate_id['id']])->select();
        $content['first']=array('value'=>input('first'),'color'=>'#000000');
        foreach ($variables as $variable) {
            $content[$variable['key']]=array('value'=>$variable['variable'],'color'=>$variable['color']);
        }
        $content['remark']=array('value'=>input('remark'),'color'=>'#000000');
        $param['content']=json_encode($content);
        $res=$this->model->where(['id'=>$id])->update($param);
        if($res){
            return array(
                'success'=>true,
                'message'=>'保存成功'
            );
        }else{
            return array(
                'success'=>false,
                'message'=>'保存失败'
            );
        }
    }
//    public function update_data($param,$key,$value,$color,$id){
//        $content=array();
//        //构造消息内容
//        $value_arr=array();
//        for($j=0;$j<count($value);$j++){
//            $item=Db::name('wechat_template_variable')->where(['text'=>$value[$j]])->find();
//            $value_arr[]=$item['variable'];
//        }
//
//        $content['first']=array('value'=>input('first'),'color'=>'#000000');
//        for ($i=0;$i<count($key);$i++){
//            $content[$key[$i]]=array('value'=>str_replace('"', '', $value_arr[$i]),'color'=>$color[$i]);
//        }
//        $content['remark']=array('value'=>input('remark'),'color'=>'#000000');
//        $param['content']=json_encode($content);
//        $res=$this->model->where(['id'=>$id])->update($param);
//        if($res){
//            return array(
//                'success'=>true,
//                'message'=>'保存成功'
//            );
//        }else{
//            return array(
//                'success'=>false,
//                'message'=>'保存失败'
//            );
//        }
//    }

    public function delete_data($id){
        $res=$this->model->where(['id'=>$id])->delete();

        if($res){
            return array(
                'success'=>true,
                'message'=>'删除成功'
            );
        }else{
            return array(
                'success'=>false,
                'message'=>'删除失败'
            );
        }
    }


    public function get_content_data($param){
        $new_arr=array();
        $arr=array();
        $model=new WechatTemplateVariable();
        foreach ($param as $key=>$value){
            if($key!="first" && $key!="remark"){
                $new_arr['key']=$key;
                $valiable=$model->where(['variable'=>$value['value']])->find();
                $new_arr['value']=$valiable['text'];
                $new_arr['color']=$value['color'];
                $arr[]=$new_arr;
            }

        }
        return $arr;
    }

    public function change_data_status($param){
        $res=$this->model->update($param);
        if($res){
            return array(
                'success'=>true,
                'message'=>'保存成功'
            );
        }else{
            return array(
                'success'=>false,
                'message'=>'保存失败'
            );
        }
    }

}